package com.toutou.kotlin

import android.os.Bundle
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.toutou.base.activity.BaseActivity
import com.toutou.base.adapter.BaseEmptyAdapterParent
import com.toutou.base.adapter.BaseRecyclerAdapter
import com.toutou.base.been.ChatModule
import com.toutou.kotlin.databinding.ActivityMainBinding
import com.toutou.kotlin.viewholder.ChatItemViewHolder
import com.toutou.kotlin.vm.MainVM


class MainActivity : BaseActivity<MainVM, ActivityMainBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.vm = mViewModel.apply {
            sendRequest.observe(this@MainActivity) {
                mDatabind.adapter?.add(it)
            }
            sendResult.observe(this@MainActivity) {
                if (it.error == null){
                    mDatabind.adapter?.add(it)
                }
            }
        }
        mDatabind.adapter = BaseEmptyAdapterParent(this, object : BaseRecyclerAdapter.NormalAdapterDelegate{
            override fun getItemViewType(position: Int): Int {
                return 1
            }

            override fun onCreateViewHolder(
                parent: ViewGroup,
                viewType: Int
            ): RecyclerView.ViewHolder {
                return ChatItemViewHolder(parent)
            }

            override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
                (holder as ChatItemViewHolder).bindData(mDatabind.adapter?.getItem(position) as ChatModule)
            }
        })
    }


}